package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.dao.Database;
import com.beerandjesus.sportofkings.handicapper.model.MaidenHorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.Sire;
import com.beerandjesus.sportofkings.handicapper.model.TrackSurface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class SireFirstTimeStarterPercentageResolver {

    public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

    @Autowired
    private Database database;

    public void resolveSireWinPercentage(MaidenHorseAnalysisResultImpl result, String sireName, TrackSurface trackSurface) {

        switch (trackSurface) {
            case DIRT:
            case INNER_DIRT:
                result.setSireWinPercentage(winPercentageToString(getDatabase().getDirtArtificialSireByName(sireName)));
                break;
            case TURF:
            case INNER_TURF:
                result.setSireWinPercentage(winPercentageToString(getDatabase().getTurfSireByName(sireName)));
        }
    }

    private String winPercentageToString(Sire sire) {
        if (sire == null) return null;
        BigDecimal percentage = sire.getWinPercentage();
        if (percentage == null) return null;
        return percentage.multiply(ONE_HUNDRED).intValue() + "%";
    }

    public Database getDatabase() {
        return database;
    }

    public void setDatabase(Database database) {
        this.database = database;
    }
}
